﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    layui.use(['jquery', 'form', 'laydate', 'common','optimizeSelectOption','xmSelect'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            laydate = layui.laydate;
        var keyValue = $.request("keyValue");
        let stopType = $.request("stopType");
        //权限字段
        common.authorizeFields('adminform');


        var xmSelect = layui.xmSelect;



        laydate.render({
            elem: '#F_PlanStopStartTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
            //value: new Date()
        });

        laydate.render({
            elem: '#F_PlanStopEndTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
            //value: new Date()
        });

        laydate.render({
            elem: '#F_RealStopStartTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
            //value: new Date()
        });


        laydate.render({
            elem: '#F_RealStopEndTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
            //value: new Date()
        });

        //渲染多选
        var F_RepeatWeekday = xmSelect.render({
            el: '#F_RepeatWeekday',
            tips: "永不",
            toolbar: {
                show: true,
            },
            autoRow: true,
            on: function (data) {

                $('#F_PlanStopEndTime').remove();
                $('#F_PlanStopStartTime').remove();
                $('.F_PlanStopStartTime').html(' <input type="text" id="F_PlanStopStartTime" name="F_PlanStopStartTime" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">');
                $('.F_PlanStopEndTime').html('<input type="text" id="F_PlanStopEndTime" name="F_PlanStopEndTime" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">');

                //arr:  当前多选已选中的数据
                var arr = data.arr;
                console.log(arr.length);
                if (arr.length > 0) {
                    laydate.render({
                        elem: '#F_PlanStopStartTime'
                        , trigger: 'click'
                        , type: 'time'
                        , format: 'yyyy/MM/dd HH:mm:ss',
                        //value: new Date()
                    });

                    laydate.render({
                        elem: '#F_PlanStopEndTime'
                        , trigger: 'click'
                        , type: 'time'
                        , format: 'yyyy/MM/dd HH:mm:ss',
                        //value: new Date()
                    });
                } else {
                    laydate.render({
                        elem: '#F_PlanStopStartTime'
                        , trigger: 'click'
                        , type: 'datetime'
                        , format: 'yyyy/MM/dd HH:mm:ss',
                        //value: new Date()
                    });

                    laydate.render({
                        elem: '#F_PlanStopEndTime'
                        , trigger: 'click'
                        , type: 'datetime'
                        , format: 'yyyy/MM/dd HH:mm:ss',
                        //value: new Date()
                    });
                }
                //form.render();

            },
            data: [
                { name: '周一', value: 1 },
                { name: '周二', value: 2 },
                { name: '周三', value: 3 },
                { name: '周四', value: 4 },
                { name: '周五', value: 5 },
                { name: '周六', value: 6 },
                { name: '周日', value: 7 },
            ]
        });

        var F_EqpId;

        $(function () {
            initControl();
            $.ajax({
                type: 'get',
                url: "/EquipmentManage/EqpPqcCheckItem/GetEqpListJsonxmSelect",
                //dataType: 'json',
                success: function (data) {
                   let data1 = JSON.parse(data);
                    if (data1 != null) {
                        F_EqpId = xmSelect.render({
                            el: '#F_EqpId',
                            tips: "请选择设备(可多选)",
                            toolbar: {
                                show: true,
                            },
                            name: 'lalalalalala',
                            layVerify: 'required',
                            layVerType: 'msg',
                            autoRow: true,
                            data: data1
                        });
                    }
                }
            })

            //if (!!keyValue) {
            //     common.ajax({
            //       url: '/EquipmentManage/EqpStopRecord/GetFormJson',
            //       dataType: 'json',
            //       data: { keyValue: keyValue },
            //       async: false,
            //       success: function (data) {
            //           common.val('adminform', data);

            //           let a = data.F_RepeatWeekday.split(",");
            //           F_RepeatWeekday.setValue(a);

            //        }
            //   });
            //}
            if (stopType == 1) {
                $('#time').addClass("layui-hide");
                $('#repeat').addClass("layui-hide");
                $('#F_PlanStopStartTime').removeAttr("lay-verify");
                $('#F_PlanStopEndTime').removeAttr("lay-verify");
                $('#F_RealStopStartTime').attr("lay-verify", "required");
                $('#F_RealStopEndTime').attr("lay-verify", "required");

            } else if (stopType == 2) {

            }

           form.render();
       });
       wcLoading.close();

       function initControl() {
           //layui.$('#F_EqpId').bindSelect({
           //    url: "/EquipmentManage/EqpPqcCheckItem/GetEqpListJson",
           //    id: "F_Id",
           //    text: "F_EqpName"
           //});
        }


        form.on('select(splitType)', function (data) {

            $('#F_EqpName').val($("#F_EqpId option:selected").text());
            form.render();
        });

        form.on('select(stopTypeFliter)', function (data) {

            if ($('#F_StopType option:selected').val() == "1") {
                $('#time').addClass("layui-hide");
                $('#repeat').addClass("layui-hide");
                $('#F_PlanStopStartTime').removeAttr("lay-verify");
                $('#F_PlanStopEndTime').removeAttr("lay-verify");

                $('#F_RealStopStartTime').attr("lay-verify", "required");
                $('#F_RealStopEndTime').attr("lay-verify", "required");

            } else {
                $('#time').removeClass("layui-hide");
                $('#repeat').removeClass("layui-hide");
                $('#F_PlanStopStartTime').attr("lay-verify","required");
                $('#F_PlanStopEndTime').attr("lay-verify", "required");

                $('#F_RealStopStartTime').removeAttr("lay-verify");
                $('#F_RealStopEndTime').removeAttr("lay-verify");

            }

            form.render();
        });



       //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (data.field.F_StopType == 2) {
                var oDate1 = new Date(data.field.F_PlanStopStartTime);
                var oDate2 = new Date(data.field.F_PlanStopEndTime);
                if (oDate1 > oDate2) {
                    common.modalMsg("结束时间不能早于开始时间", "warning");
                    return false;
                }

                if (data.field.F_RealStopStartTime && data.field.F_RealStopEndTime) {
                    var oDate1 = new Date(data.field.F_RealStopStartTime);
                    var oDate2 = new Date(data.field.F_RealStopEndTime);
                    if (oDate1 > oDate2) {
                        common.modalMsg("结束时间不能早于开始时间", "warning");
                        return false;
                    }
                }

            }
            else if (data.field.F_StopType == 1) {
                var oDate1 = new Date(data.field.F_RealStopStartTime);
                var oDate2 = new Date(data.field.F_RealStopEndTime);
                if (oDate1 > oDate2) {
                    common.modalMsg("结束时间不能早于开始时间", "warning");
                    return false;
                }

            }

            if (F_EqpId.getValue('valueStr') == '') {
                common.modalMsg("设备不能为空！", "warning");
                return false;
            }

            var postData = data.field;
            postData.F_RepeatWeekday = F_RepeatWeekday.getValue('valueStr');
            postData.F_EqpId = F_EqpId.getValue('valueStr');
            postData.F_EqpName = F_EqpId.getValue('nameStr');
           common.submitForm({
               url: '/EquipmentManage/EqpStopRecord/SubmitForm?keyValue=' + keyValue,
               param: postData,
               success: function () {
                   common.parentreload('data-search-btn');
               }
           })
           return false;
       });
   });
</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item">
                    <label class="layui-form-label required">设备</label>
                    <div class="layui-input-block">
                        @*<select id="F_EqpId" name="F_EqpId" lay-filter="splitType" lay-search lay-verify="required">
            <option value="" selected>请选择</option>
        </select>*@
                        <div id="F_EqpId"></div>
                    </div>
                    <input type="hidden" id="F_EqpName" name="F_EqpName" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">停机类型</label>
                    <div class="layui-input-block">
                        <select id="F_StopType" name="F_StopType" lay-search lay-verify="required" lay-filter="stopTypeFliter">
                            <option value="" selected>请选择</option>
                            <option value="1">实际停机</option>
                            <option value="2">计划内停机</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item" id="repeat">
                    <label class="layui-form-label">重复</label>
                    <div class="layui-input-block">
                        <div id="F_RepeatWeekday"></div>
                    </div>
                </div>

                <div id="time">
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">计划停机开始时间</label>
                        <div class="layui-input-block F_PlanStopStartTime">
                            <input type="text" id="F_PlanStopStartTime" name="F_PlanStopStartTime" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label required">计划停机结束时间</label>
                        <div class="layui-input-block F_PlanStopEndTime">
                            <input type="text" id="F_PlanStopEndTime" name="F_PlanStopEndTime" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">停机原因分类</label>
                    <div class="layui-input-block">
                        <select id="F_StopReasonType" name="F_StopReasonType" lay-search lay-verify="required">
                            <option value="" selected>请选择</option>
                            <option value="1">人</option>
                            <option value="2">机</option>
                            <option value="3">料</option>
                            <option value="4">法</option>
                            <option value="5">环</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">停机原因</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_StopReason" name="F_StopReason" autocomplete="off" lay-verify="required" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">实际停机开始时间</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_RealStopStartTime" name="F_RealStopStartTime" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">实际停机结束时间</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_RealStopEndTime" name="F_RealStopEndTime" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">解决措施</label>
                    <div class="layui-input-block">
                        <input type="text" id="F_Solution" name="F_Solution" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</body>

